package controllers.administrator;

import java.util.Collection;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.ActorService;
import services.TeamService;
import controllers.AbstractController;
import domain.Team;


@Controller
@RequestMapping("/group/admin")
public class TeamAdministratorController extends AbstractController {

	//Services -----------------------------------------------------------------

	@Autowired
	ActorService actorService;
	
	@Autowired
	TeamService groupService;


	// Constructors -----------------------------------------------------------

	public TeamAdministratorController() {
		super();
	}

	// List --------------------------------------------------------------------
	
	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(required=false) String message) {
		ModelAndView result;
		Collection<Team> groups = groupService.findAllGroups();
		
		result = new ModelAndView("group/list");
		result.addObject("groups", groups);
		result.addObject("message", message);
		return result;
	}
	
	// Creation ---------------------------------------------------------------		

	@RequestMapping("/create")
	public ModelAndView create() {
		ModelAndView result;
		Team group;
	
		group = groupService.create();
		
		result = createEditModelAndView(group);
		

		return result;
		
	
	}
	
	// Edition ----------------------------------------------------------------
	
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam int groupId) {
		ModelAndView result;
		Team group;

		group = groupService.findOne(groupId);		
		result = createEditModelAndView(group);

		return result;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
	public ModelAndView save(@Valid Team group, BindingResult bindingResult) {
		ModelAndView result;		

		if (bindingResult.hasErrors()) {
			result = createEditModelAndView(group);
		} else {
			try {
				groupService.save(group);			
				result = new ModelAndView("redirect:/admin/group/list.do");
				result.addObject("message", "group.commit.success");
			} catch (Throwable oops) {
				result = createEditModelAndView(group, oops.getMessage());
			}
		}

		return result;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "delete")
	public ModelAndView delete(Team group) {
		ModelAndView result;		

		try {
			groupService.delete(group);	
			result = new ModelAndView("redirect:/admin/group/list.do");
			result.addObject("message", "group.delete.success");
		} catch (Throwable oops) {
			result = createEditModelAndView(group, oops.getMessage());
		}

		return result;
	}
	
	// Ancillary methods ------------------------------------------------------

	protected ModelAndView createEditModelAndView(Team group) {
		ModelAndView result;

		result = createEditModelAndView(group, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(Team group, String message) {
		ModelAndView result;

		result = new ModelAndView("group/edit");
		result.addObject("group", group);
		result.addObject("message", message);

		return result;
	}

}
